Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Drawing.Image
Imports System.Drawing.Imaging
Imports System.IO

Namespace DotNetNuke.Modules.ProjectTracker

    Public Class GraphicsProcessor
        Inherits System.Web.UI.Page

#Region "Public Functions"

        Public Sub RatingImage(ByVal normalImage As String, ByVal selectedImage As String, ByVal count As Integer, ByVal rating As Integer)
            Dim _imageHeight As Integer
            Dim _imageWidth As Integer
            Dim _ptr As Integer
            Dim i As Integer

            Dim _outputImage As Bitmap
            Dim _outputCanvas As Graphics

            ' get the size of the normal image
            Dim _normalImage As New Bitmap(Server.MapPath("~/DesktopModules/ProjectTracker/images/" & normalImage))
            Dim _normalImageSize As SizeF = _normalImage.PhysicalDimension()

            ' get the size of the selected image
            Dim _selectedImage As New Bitmap(Server.MapPath("~/DesktopModules/ProjectTracker/images/" & selectedImage))
            Dim _selectedImageSize As SizeF = _selectedImage.PhysicalDimension()

            ' calc the overall size of the image to be generated
            If _normalImage.Height > _selectedImage.Height Then
                _imageHeight = _normalImage.Height
            Else
                _imageHeight = _selectedImage.Height
            End If

            _imageWidth = (_selectedImage.Width * rating)
            _imageWidth += (_normalImage.Width * (count - rating))

            ' create the output image
            _outputImage = New Bitmap(_imageWidth, _imageHeight)
            _outputCanvas = Graphics.FromImage(_outputImage)

            ' place the correct number of selected images
            _ptr = 0
            For i = 1 To rating
                _outputCanvas.DrawImage(_selectedImage, _ptr, 0)
                _ptr += _selectedImage.Width
            Next

            ' append the remaining images using the normal image
            For i = 1 To (count - rating)
                _outputCanvas.DrawImage(_normalImage, _ptr, 0)
                _ptr += _normalImage.Width
            Next

            ' output the image to the browser
            _outputImage.Save(Response.OutputStream, ImageFormat.Jpeg)

            ' cleanup
            _outputImage.Dispose()
            _outputCanvas.Dispose()
            _normalImage.Dispose()
            _selectedImage.Dispose()

        End Sub

#End Region

#Region "Event Handlers"

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            ' get params
            Dim functionName As String = ""
            If Not Request.QueryString("f") Is Nothing Then
                functionName = Request.QueryString("f").ToString()
            End If

            Try
                Select Case functionName
                    Case "RatingImage"
                        ' get the image params
                        Dim lImage As String = Request.QueryString("image").ToString()
                        Dim lSelected As String = Request.QueryString("selected").ToString()
                        Dim lCount As Integer = Integer.Parse(Request.QueryString("count").ToString())
                        Dim lRating As Integer = Integer.Parse(Request.QueryString("rating").ToString())
                        RatingImage(lImage, lSelected, lCount, lRating)
                End Select
            Catch ex As Exception

            End Try

        End Sub

#End Region

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

    End Class

End Namespace
